package com.linciping.useges.plugin.view;

import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiElement;
import com.intellij.ui.tree.ui.DefaultTreeUI;
import com.linciping.useges.plugin.bean.UsegesNode;

import javax.swing.*;
import javax.swing.tree.TreeModel;

public class UsegesView {
    private JTree treeUseges;
    private JPanel plRoot;

    public UsegesView(TreeModel treeModel) {
        initTree(treeModel);
    }

    private void initTree(TreeModel treeModel) {
        treeUseges.setEditable(false);
        treeUseges.setModel(treeModel);
        treeUseges.setUI(new DefaultTreeUI());
        UsegesTreeCellRendererV2 defaultTreeCellRenderer = new UsegesTreeCellRendererV2();
        treeUseges.setCellRenderer(defaultTreeCellRenderer);
        treeUseges.addTreeSelectionListener(e -> {
            UsegesNode node = (UsegesNode) treeUseges
                    .getLastSelectedPathComponent();
            if (node == null) return;
            if (node.canNavigate()){
                Navigatable navigatable = (Navigatable) node.getPsiNamedElement();
                navigatable.navigate(true);
            }
        });
    }

    public JComponent getContent() {
        return plRoot;
    }
}
